package com.deeper.customview.ui.c5.demo.c05d03;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

import java.util.Random;

public class CodeView extends View {
    private static final int DEFAULT_COUNT = 4;
    private static final int DEFAULT_LINE_COUNT = 50;
    private static final float DEFAULT_FONT_SIZE = 12; //sp
    private static final int DEFAULT_COLOR = Color.BLACK;
    private int count;
    private int lineCount;
    private int fontSize;
    private int color;
    private Random random;
    private Paint paint;
    private String code;


    public CodeView(Context context) {
        super(context);
    }


    public CodeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public CodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CodeView);
        count = typedArray.getInteger(R.styleable.CodeView_count, DEFAULT_COUNT);
        lineCount = typedArray.getInteger(R.styleable.CodeView_line_count, DEFAULT_LINE_COUNT);
        fontSize = typedArray.getDimensionPixelSize(R.styleable.CodeView_font_size,
                (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, DEFAULT_FONT_SIZE, getResources().getDisplayMetrics())));
        color = typedArray.getColor(R.styleable.CodeView_code_color, DEFAULT_COLOR);
        typedArray.recycle();

        random = new Random();
        paint = new Paint();
        initPaint();
        code = getCode();
    }


    private void initPaint() {
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setTextSize(fontSize);
    }


    private String getCode() {
        String str = "";
        for (int i = 0; i < count; i++) {
            str += random.nextInt(10);
        }
        return str;
    }


    private int measureWidth(int widthMeasureSpec, Rect textRect) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;

        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            width = getPaddingLeft() + textRect.width() + getPaddingRight();
        }
        return width;
    }


    private int measureHeight(int heightMeasureSpec, Rect textRect) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;

        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            height = getPaddingTop() + textRect.width() + getPaddingBottom();
        }
        return height;
    }


    private Rect getTextRect() {
        //根据Paint设置的绘图参数计算文字所占宽度
        Rect rect = new Rect();
        //文字所占区域大小保存在rect中
        paint.getTextBounds(code, 0, code.length(), rect);
        return rect;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Rect textRect = getTextRect();
        int width = this.measureWidth(widthMeasureSpec, textRect);
        int height = this.measureHeight(heightMeasureSpec, textRect);
        setMeasuredDimension(width, height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        Rect rect = new Rect(0, 0, width, height);

        //绘制外围矩阵
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        Rect rect1 = new Rect(rect);
        rect1.inset(2, 2);
        canvas.drawRect(rect1, paint);
        //绘制随机干扰线
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GRAY);
        for (int i = 0; i <= lineCount; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            canvas.drawLine(x1, y1, x2, y2, paint);
        }
        //绘制文字
        paint.setColor(color);
        Rect textRect = getTextRect();
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        int x = (width - textRect.width()) / 2;
        int y = (int) (height / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent);
        canvas.drawText(code, x, y, paint);
    }


    //------互动部分---------
    public int getCount() {
        return count;
    }


    public void setCount(int count) {
        this.count = count;
        code = getCode();
        requestLayout();//重新调整布局大小
    }


    public int getLineCount() {
        return lineCount;
    }


    public void setLineCount(int count) {
        this.lineCount = count;
        invalidate();
    }


    public int getFontSize() {
        return fontSize;
    }


    public void setFontSize(int fontSize) {
        this.fontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                fontSize, getResources().getDisplayMetrics());
        initPaint();
        requestLayout();
    }


    public int getColor() {
        return color;
    }


    public void setColor(int color) {
        this.color = color;
        initPaint();
        invalidate();
    }


    public void refresh() {
        code = getCode();
        invalidate();
    }


    public String theCode() {
        return code;
    }
}
